package xkp.lesson2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestNIO2 {
    public static void main(String[] args) throws IOException {

        FileInputStream inputStream = new FileInputStream("Lesson30/file/hosts");
        FileOutputStream outputStream = new FileOutputStream("Lesson30/file/hosts2");
        //获取通道
        FileChannel channel1 = inputStream.getChannel();
        FileChannel channel2 = outputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(64);

        int length = channel1.read(buffer);

        while(length != -1){

            buffer.flip();//此处把position返回到0位置，才能开始读取  //1
            channel2.write(buffer);
            buffer.clear(); //2

            length = channel1.read(buffer);
        }

        channel1.close();
        channel2.close();
    }

}
